/**
 *  Copyright 2022 Martynas Jusevičius <martynas@atomgraph.com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.atomgraph.linkeddatahub.server.security;

import com.atomgraph.linkeddatahub.model.auth.Authorization;
import com.atomgraph.linkeddatahub.vocabulary.ACL;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.vocabulary.RDF;

/**
 * Authorization context.
 * 
 * @author {@literal Martynas Jusevičius <martynas@atomgraph.com>}
 */
public class AuthorizationContext
{

    private final Model authorizationModel;
    
    /**
     * Constructs authorization context from RDF model with WAC authorizations.
     * 
     * @param authorizationModel WAC authorization data
     */
    public AuthorizationContext(Model authorizationModel)
    {
        this.authorizationModel = authorizationModel;
    }
    
    /**
     * Returns the URIs of all authorization modes in this context.
     * 
     * @return URIs of auth modes
     */
    public Set<URI> getModeURIs()
    {
        Set<URI> modeURIs = new HashSet<>();
        
        ResIterator it = authorizationModel.listSubjectsWithProperty(RDF.type, ACL.Authorization);
        try
        {
            while (it.hasNext())
            {
                Authorization auth = it.next().as(Authorization.class);
                modeURIs.addAll(auth.getModeURIs());
            }
            
            return modeURIs;
        }
        finally
        {
            it.close();
        }
    }
    
}
